package doInterce.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import doInterce.DoInterface;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 给越秀区的机构数进行排序
 * @Author: syh
 * @Date: 2023/12/18/11:32
 */
public class DoTree2 implements DoInterface {
    public void doExecute() {
        Map<String, List<String>> stringListMap = readFileContext();
        for(String key : stringListMap.keySet()){
            stringListMap.get(key).sort((e1,e2)->{
                String[] arr1 = e1.split("&&&&");
                String[] arr2 = e2.split("&&&&");

                return Integer.compare(Integer.parseInt(arr1[2]),Integer.parseInt(arr2[2]));
            });
        }
        printTree(stringListMap,"-1&&&&测试");
    }

    public Map<String,List<String>> readFileContext(){
        Map<String,List<String>> resultList = new HashMap<>();
        List<String> list = FileUtil.readLines("D:\\360安全浏览器下载\\单位信息备份[2023年公务员统计]20240115.txt", "utf-8");
        for (String str:list) {
            String[] arr = str.split(" ");
            String b0111 = arr[0];
            String b0121 = arr[1];
            String b0101 = arr[2];
            int orderNum = Convert.toInt(arr[4]);

            if(StringUtils.isNotBlank(b0111) && StringUtils.isNotBlank(b0121)){
                if(resultList.containsKey(b0121)){
                    resultList.get(b0121).add(b0111+"&&&&"+b0101+"&&&&"+orderNum);
                }else{
                    List<String> list1 = new ArrayList<>();
                    list1.add(b0111+"&&&&"+b0101+"&&&&"+orderNum);
                    resultList.put(b0121,list1);
                }
            }
        }

        return resultList;
    }
//县市区旗
    public void printTree(Map<String,List<String>> map , String b0111){
        String[] split = b0111.split("&&&&");
        b0111 = split[0];
        System.out.println(split[1].endsWith("街")? split[1]+"(街道)":split[1]);
        if(map.containsKey(b0111) && CollectionUtil.isNotEmpty(map.get(b0111))){
            for(String childrenB0111 : map.get(b0111)){
                printTree(map,childrenB0111);
            }
        }
    }
}
